package com.zjk.storage.core.aspect.upload;

import com.zjk.storage.core.aspect.FileStorageAspect;
import com.zjk.storage.core.entity.BaseFileInfo;
import com.zjk.storage.core.entity.UploadPretreatment;
import com.zjk.storage.core.provider.FileStorage;
import lombok.Getter;
import lombok.Setter;

import java.util.Iterator;


@Setter
@Getter
public class UploadAspectChain {

    private UploadAspectChainCallback callback;
    private Iterator<FileStorageAspect> aspectIterator;

    public UploadAspectChain(Iterable<FileStorageAspect> aspects, UploadAspectChainCallback callback) {
        this.aspectIterator = aspects.iterator();
        this.callback = callback;
    }

    public BaseFileInfo next(BaseFileInfo fileInfo, UploadPretreatment pre, FileStorage fileStorage) {
        if (aspectIterator.hasNext()) {
            return aspectIterator.next().uploadAround(fileInfo, pre, fileStorage, this);
        }
        return callback.run(fileInfo, pre, fileStorage);
    }

}
